{extend name="public/pure"}
{block name="style"}
<title>管理收货地址</title>
<link rel="stylesheet" href="/home/css/address/ydui.css">
<link rel="stylesheet" href="/home/css/address/add.css">

{/block}
{block name="body"}
<div class="lists" >
    <div class="list">
        <span class="fl title">收货人</span>
        <input class="fl name" type="text" name="addressee"  placeholder="姓名"  value="{$info.addressee || default=''}"/>
    </div>
    <div class="list">
        <span class="fl title">联系电话</span>
        <input class="fl name" type="text" name="tel" placeholder="手机或固话" value="{$info.tel || default=''}"/>
    </div>
    <div class="list">
        <span class="fl title">选择地区</span>
        <input type="text" class="cell-input fl name" name="address1" readonly id="J_Address"  value="{$info.address1 || default=''}" placeholder="请选择收货地址">
    </div>
    <div class="list site">
        <span class="fl title">详细地址</span>
        <textarea  class="fl name" name="address2"  placeholder="请输入详细地址信息，如道路、门牌号、小区、楼栋号、单元室等" >{$info.address2 || default=''}</textarea>
    </div>
    <div class="list">
        <span class="fl default1">设置为默认地址</span>
        {if condition="($info['default_address'] == 1)"}
        <img class="fr img" src="/home/images/commodity/rectangle_.png"/>
        {else /}
        <img class="fr img" src="/home/images/commodity/rectangle.png"/>
        {/if}


    </div>

</div>
<div class="button">
    <input type="button" class='btn' value="保存"/>
</div>
{/block}
{block name="script"}
<script src="/home/js/ydui.citys.js"></script>
<!--<script src="/home/js/ydui.flexible.js"></script>-->
<script src="/home/js/ydui.js"></script>
<script>

    // 三级联动
    !function () {
        var $target = $('#J_Address');

        $target.citySelect();

        $target.on('click', function (event) {
            event.stopPropagation();

            // 先判断手机号
            if ((/^1[34578]\d{9}$/.test($("input[name='tel']").val()))||/^(([0\+]\d{2,3}-)?(0\d{2,3})-)(\d{7,8})(-(\d{3,}))?$/.test($("input[name='tel']").val())) {
                $target.citySelect('open');
            }else {
                swal({
                    title: ' ',
                    text: '请先填写正确的手机号码或固话！',
                    type: 'warning',
                    showConfirmButton:false,
                    timer:1500
                });
            }
        });

        $target.on('done.ydui.cityselect', function (ret) {
            $(this).val(ret.provance + ' ' + ret.city + ' ' + ret.area);
        });
    }();

    // 默认选择
    $('.img').on('click',function(){
        if($(this).attr('src')=='/home/images/commodity/rectangle_.png'){
            $(this).attr('src','/home/images/commodity/rectangle.png');
        }else{
            $(this).attr('src','/home/images/commodity/rectangle_.png');
        }
    });

    // 手机号码判断
    $("input[name='tel']").on("blur", function (e) {
        var val = $(this).val();
        if((/^1[34578]\d{9}$/.test(val))||/^(([0\+]\d{2,3}-)?(0\d{2,3})-)(\d{7,8})(-(\d{3,}))?$/.test(val)) {

        }else{
            swal({
                title: ' ',
                text: '请输入正确的手机号码！',
                type: 'error',
                showConfirmButton:false,
                timer:1500
            });
            $("input[name='tel']").val('');
            $("input[name='tel']").focus();
        }
        return false;
    });

    // 提交
    $('.btn').off('click').on('click',function(){
        var id = '{$info.id}';
        var name=$("input[name='addressee']").val();
        var phone=$("input[name='tel']").val();
        var chose=$("input[name='address1']").val();
        var address=$("textarea[name='address2']").val();
        var default1;
        if($('.img').attr('src')=='/home/images/commodity/rectangle_.png'){
            default1=1;
        }else{
            default1=0;
        }
        if(name != "" && phone != "" && chose != "" && address != ""){
            $.ajax({
                type: "post",
                url:"{:Url('Address/add')}",
                data: {
                    id: id,
                    addressee: name,
                    tel: phone,
                    address: chose+' '+address,
                    address1: chose,
                    address2: address,
                    default_address:default1
                },
                success: function(data){
                    //成功后刷新页面
                    if(data.code == 1){
                        swal({
                            type: "success",
                            title: "",
                            text:"提交成功",
                            timer: 1500,
                            showConfirmButton: false
                        });
                        setTimeout(function () {
                            history.go(-1);
                        },1500);
                        return false;
                    }else{
                        setTimeout(function(){
                            swal({
                                type: "error",
                                title: "",
                                text:"提交失败",
                                timer: 1500,
                                showConfirmButton: false
                            })
                        },200);
                    }
                }
            })
        }else{
            swal({
                type: "error",
                title: "",
                text:"请完善内容",
                timer: 1500,
                showConfirmButton: false
            });
        }
    })
</script>
{/block}